package com.finance.rule.computer;

import com.finance.base.enums.MajorTypeEnum;
import com.finance.report.bean.pojo.LogisticsData;
import com.finance.report.service.ILogisticsDataService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 三同合一
 * 同收货地址 同收货电话 同妥投时间
 */
@Component
@Scope("prototype")
public class AddressNameSignday3MetaAnalyzer implements IMajorAnalyzer,Serializable {
    @Autowired
    private ILogisticsDataService logisticsDataService;

    @Override
    public boolean isSingle(LogisticsData logisticsData) {
        return CollectionUtils.isEmpty(this.getOtherParts(logisticsData));
    }
    @Override
    public MajorTypeEnum majorTyeOf(LogisticsData logisticsData) {
        List<LogisticsData> otherParts = this.getOtherParts(logisticsData);
        if(CollectionUtils.isEmpty(otherParts)) {
            return MajorTypeEnum.MAJOR;
        }
        otherParts.add(logisticsData);
        Collections.sort(otherParts, new Comparator<LogisticsData>() {
            @Override
            public int compare(LogisticsData o1, LogisticsData o2) {
                int result =  o1.getSignTime().equals(o2.getSignTime()) ? 0 : o1.getSignTime().before(o2.getSignTime()) ? -1 : 1;
                result = result==0 ? o1.getWaybillNum().compareTo(o2.getWaybillNum()) : result;
                return result;
            }
        });
        boolean major = logisticsData.equals(otherParts.get(0));
        return major? MajorTypeEnum.MAJOR:MajorTypeEnum.DRAG;
    }

    @Override
    public List<LogisticsData> getOtherParts(LogisticsData logisticsData) {
        String tableName = logisticsDataService.getDataTableName(logisticsData.getSignTime());
        String nameConsignee = logisticsData.getNameConsignee();
        String addressConsignee = logisticsData.getAddressConsignee();
        List<LogisticsData> list = logisticsDataService.addressNameSignday3Meta(addressConsignee,nameConsignee,logisticsData.getAcceptTime(),tableName);
        if(CollectionUtils.isEmpty(list)|| list.size()<=1 ) {
            return new ArrayList<LogisticsData>();
        }
        list.remove(logisticsData);
        return list;
    }


    @Override
    public void init(String json) {

    }
}
